/**
 * @file app.js
 * @author swan
 */

/* globals swan */

App({
    globalData: {
        userInfo: 'user',
        info: {},
        currentNum: -1,
        sysinfo: {},
    },
    onLaunch(options) {
        this.globalSetting();
        if (swan.canIUse('showFavoriteGuide')) {
            swan.showFavoriteGuide({
                type: 'bar',
                content: '一键添加到我的小程序',
                success(res) {
                    console.log('添加成功：', res);
                },
                fail(err) {
                    console.log('添加失败：', err);
                }
            });
        }
    },
    onShow(options) {
        // do something when show
    },
    onHide() {
        // do something when hide
    },
    siteInfo: require('./siteinfo.js'),
    util: require('./unit/unit.js'),
    WxParse:require('./wxParse/wxParse.js'),

    // time:require('./utils/time.js'),
    globalSeo:function(type,cid,id){
        var that = this;
        return new Promise(function (resolve, reject) {
            swan.request({
                    url: that.util.url('entry/baiduapp/Seo'),
                    method: 'GET',
                    dataType: 'json',
                    data: {
                        uniacid: that.siteInfo.uniacid,
                        type: type,
                        cid:cid,
                        id:id,
                        m: that.siteInfo.mName,
                    },
                    header: {
                        'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                        var seoInfo = res.data.data.info;
                        // console.log(seoInfo);
                        resolve(seoInfo);

                    },
                    fail: function (err) {
                        reject(err);
                        console.log('错误码：' + err.errCode);
                        console.log('错误信息：' + err.errMsg);
                    }
                });

        });
    },
    globalSetting: function () {
       
        var that = this;

        return new Promise(function (resolve, reject) {
            var sysinfo = swan.getStorageSync('info');
            if (sysinfo == '') {
                var sysinfourl = that.util.url('entry/baiduapp/Sysinfo');
                swan.request({
                    url: sysinfourl,
                    method: 'GET',
                    dataType: 'json',
                    data: {
                        uniacid: that.siteInfo.uniacid,
                        m: that.siteInfo.mName,
                    },
                    header: {
                        'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                        var info = res.data.data.info;

                        swan.setStorageSync('info', res.data.data.info);
                        that.head_backgroup(info);


                        resolve(info);
                    },
                    fail: function (err) {
                        reject(err);
                        console.log('错误码：' + err.errCode);
                        console.log('错误信息：' + err.errMsg);
                    }
                });


            } else {
                that.head_backgroup(sysinfo);
                that.globalData.sysinfo = sysinfo;
            }

        });

    },
    head_backgroup: function (sysinfo) {
        var font_color, head_color;
        (sysinfo.font_color != '' && sysinfo.font_color != '0') ? font_color = sysinfo.font_color : font_color = 'black';
        (sysinfo.head_color != '' && sysinfo.head_color != '0') ? head_color = sysinfo.head_color : head_color = '#ffffff';
        
        if(this.siteInfo.head_backgroup){
            swan.setNavigationBarColor({
                frontColor: font_color,
                backgroundColor: head_color,
            });
        }else{
            swan.setNavigationBarColor({
            frontColor: font_color
            });
        }

    },
    hide_loading:function(){
        setTimeout(function () {
          swan.hideLoading();
      },1000);
    },
    tel: function (event) {
        //打开电话
        // console.log(event);
        // var phone=this.data.blist.nav_tel
        swan.makePhoneCall({
            phoneNumber: event,
        })
    },
    driver: function () {
        //打开地图
        swan.openLocation({
            latitude: Number(this.data.blist.jing),
            longitude: Number(this.data.blist.wei),
            address: this.data.blist.address
        })
    },
    navigateMini: function (event) {
        //打开别的小程序
        var sid = event.currentTarget.dataset.sid;
        var fid = event.currentTarget.dataset.fid;
        var appid = this.data.list[fid].list[sid].appid;
        var pageaddress = this.data.list[fid].list[sid].pageaddress;
        swan.navigateToSmartProgram({
            appKey: appid,
            path: pageaddress,
            success(res) {
            }
        })
    },
    playVideo: function (event) {
        var fid = event.currentTarget.dataset.fidx;
        var sid = event.currentTarget.dataset.sidx;
        var list = this.data.list;
        list[fid]['list'][sid]['show_video'] = true;
        this.setData({
            list: list
        })
    },
    navto: function (event) {
        // console.log(11)
        //打开网页
        var nid = event.currentTarget.dataset.nid;
        var url = this.data.cats1[nid].url;

        swan.setStorageSync('navurl', url)
        if (url.indexOf('http') != '-1') {
            var url = '/pages/webview/webview?op=' + 'nav' + this.data.cats1[nid].id
        }
        swan.navigateTo({
            url: url,
        })
    },
    
    tabNav: function (event) {
        // console.log(event);
        var url = event.currentTarget.dataset.url;
        var indexId = event.currentTarget.dataset.indexId;
        var tab = event.currentTarget.dataset.tab;
        // console.log(indexId);
        this.globalData.currentNum = indexId;
        if (url.indexOf('http') != '-1') {
            // console.log('ss');
            swan.setStorageSync('navurl', url)
            //  console.log(url);

            var url = '/pages/webview/webview?op=' + tab
            swan.navigateTo({
                url: url,
            })
        } else {
            swan.navigateTo({
                url: url,
            })
        }
    },
    tonav: function (event) {
        // console.log(event);
        var url = event.currentTarget.dataset.url;
        var indexId = event.currentTarget.dataset.indexId;
        var tab = event.currentTarget.dataset.tab;
        

        this.globalData.currentNum = indexId;
    
        swan.navigateTo({
                url: url,
        })
        
    },

      //幻灯片跳转方法
  bannerUrl: function (url,id) {
    swan.setStorageSync('navurl', url)

    

    if (url.indexOf('http') != '-1') {
      var url = '../webview/webview?op=' + 'sli&slideid=' + id;
    }
   
    swan.navigateTo({
      url: url,
    })
  },
});
